using System;
using System.IO;
using System.Data;
using System.Diagnostics;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/* OWED */
using OWED.Document;
using OWED.Network;
using OWED.Network.Messages;
using OWED.Network.Messages.Reply;
using OWED.Network.Messages.General;

/// <summary>
/// Main viewdocument
/// </summary>
public partial class ViewDocument : System.Web.UI.Page {

	/// <summary>
	/// Authentication details
	/// </summary>
	String[] AuthDetails;

	/// <summary>
	/// Backend connection
	/// </summary>
	Connection conn;

	/// <summary>
	/// Getting a document
	/// </summary>
	GetDocument request;

	/// <summary>
	/// Document list reply
	/// </summary>
	DocumentReply reply;

	/// <summary>
	/// ErrorMessage for View proposal
	/// </summary>
	protected Literal pageOutput;

	/// <summary>
	/// XSLT starting process
	/// </summary>
	ProcessStartInfo PI;

	/// <summary>
	/// XSLT process
	/// </summary>
	Process p;

	/// <summary>
	/// Write XML docbook file to file system for cache
	/// </summary>
	StreamWriter WriteFile;

	/// <summary>
	/// Read XHTML output
	/// </summary>
	StreamReader ReadFile;

	protected void Page_Load(object sender, EventArgs e)
	{
		pageOutput.Text = "";
		/* check if user is logged in */
		if (!IsLoggedIn.Check(Request.Cookies["owed"])) {
			Response.Redirect("Login.aspx");
		} else {
			AuthDetails = IsLoggedIn.GetAuthentication(Request.Cookies["owed"]);
		}

		// no doc name provided
		if (Request.QueryString["document"] == null) {
			pageOutput.Text = "<h2>Error:</h2>You did not provide a document name";
			return;
		}

		// set current dir
		Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory + @"\cache\xhtml");

		// check if it's in cache
		if (!File.Exists(Request.QueryString["document"])) {

			conn = null;
			WebLogin.C(out conn, AuthDetails[0], AuthDetails[1]);
			try {
				request = new GetDocument(new User(AuthDetails[0]), new DocName(Request.QueryString["document"]));
				reply = (DocumentReply)MessageGet.GetFromXml(conn.Send(request.ToXml()));
			} catch (Exception) {
				pageOutput.Text = "<h2>Error:</h2>Document does not exist";
				return;
			}

			// write local file
			WriteFile = new StreamWriter(Request.QueryString["document"] + ".xml");
			
			WriteFile.Write(reply.ADocument.GetEncodedFormat());
			WriteFile.Close();

			// change dir
			Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory + @"\bin");

			// start xslt
			PI = new ProcessStartInfo();
			PI.FileName = AppDomain.CurrentDomain.BaseDirectory + @"\bin\" + "xsltproc.exe";
			PI.Arguments = "--output ../cache/xhtml/" + Request.QueryString["document"] + " --stringparam use.extensions 0 docbook-xsl/xhtml/docbook.xsl ../cache/xhtml/" + Request.QueryString["document"] + ".xml";

			p = new Process();
			p.StartInfo = PI;

			p.Start();
			p.WaitForExit();

		}

		// write to local cache
		Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory + @"\cache\xhtml");
		ReadFile = new StreamReader(Request.QueryString["document"]);
		pageOutput.Text = ReadFile.ReadToEnd();
		ReadFile.Close();
	}
}
